package com.gsn.poker.play;

import org.json.JSONObject;

import com.gsn.common.Settings;
import com.gsn.engine.myplay.GsnGame;
import com.gsn.engine.myplay.GsnScreen;
import com.gsn.engine.template.GsnDialogYesNoLayer.EButtonType;
import com.gsn.engine.template.GsnDialogYesNoLayer.EDialogType;
import com.gsn.engine.template.GsnDialogYesNoLayer.IDialogYesNoListener;
import com.gsn.name.StatePortal;
import com.gsn.poker.asset.PokerTexture;
import com.gsn.poker.game.DataProvider;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class PlayScreen extends GsnScreen implements IDialogYesNoListener {
	public static final String QUIT_ROOM_DLG = "quit room";
	public static final String DISCONNECT_DLG = "disconnect";
	public static final String OUT_OF_MONEY_DLG = "het tien";
	public static final String NO_PLAY_DLG = "noPlay";

	public BoardLayer boardLayer;
	public DialogLayer dialogLayer;
	public ChatLayer chatLayer;
	public JSONObject jsonJoinRoom;

	public PlayScreen(float width, float height) {
		super(width, height);
	}

	@Override
	public void setDefaultInputListener() {

		if (boardLayer != null)

			boardLayer.setInputListener();
	}

	@Override
	public void clickButton(String nameDlg, EButtonType btn) {
		switch (btn) {
		case YES:
			if (nameDlg.equals(QUIT_ROOM_DLG)) {
				MyPoker.client.send(PacketFactory.createQuitRoom());
				MyPoker.game.setChooseRoomScreen();
				MyPoker.client.send(PacketFactory.getRefreshListTable());
				// quay tro lai man hinh chon game
			} else if (nameDlg.equals(DISCONNECT_DLG)) {
				MyPoker.game.setChooseGameScreen();
				// quay tro lai man hinh chon game
			} else if (nameDlg.equals(OUT_OF_MONEY_DLG)) {
				MyPoker.game.setChooseRoomScreen();
				MyPoker.client.send(PacketFactory.getRefreshListTable());
			} else if (nameDlg.equals(NO_PLAY_DLG)) {
				MyPoker.game.setChooseRoomScreen();
				MyPoker.client.send(PacketFactory.getRefreshListTable());
			}

			break;

		default:
			break;
		}
	}

	public void showQuitDlg() {
		dialogLayer.createDialog(EDialogType.YES_NO, QUIT_ROOM_DLG, "Nếu thoát khi đang chơi, bạn sẽ mất số tiền đã cược.\n Bạn có muốn thoát?");
		dialogLayer.show();
	}

	public void showHetTien() {
		dialogLayer.createDialog(EDialogType.YES, OUT_OF_MONEY_DLG, "Bạn bị mời ra vì không đủ tiền cược!!");
		dialogLayer.show();

	}

	public void showNoPlay() {
		dialogLayer.createDialog(EDialogType.YES, NO_PLAY_DLG, "Bạn bị mời ra vì đã không chơi 3 ván liên tiếp!!");
		dialogLayer.show();

	}

	public void showDisconnectDlg() {
		if (dialogLayer != null) {
			dialogLayer.createDialog(EDialogType.YES, DISCONNECT_DLG, "Bạn bị mất kết nối!!!");
			dialogLayer.show();
		} else {
			MyPoker.game.setChooseGameScreen();
		}
	}

	public void showTakeChair(boolean success) {
		if (success)
			dialogLayer.createDialog(EDialogType.YES, "take chair", "Bạn đã đăng kí tham gia thành công!!!");
		else
			dialogLayer.createDialog(EDialogType.YES, "take chair", "Bạn đã đăng kí tham gia thất bại!!!");
		dialogLayer.show();
	}

	public void onShowScreen() {
		super.onShowScreen();

		if (jsonJoinRoom != null) {
			boardLayer.joinRoomSuccess(jsonJoinRoom);
			jsonJoinRoom = null;
		}

	}

	@Override
	public void onInit() {
		// TODO Auto-generated method stub
		boardLayer = new BoardLayer(width, height);
		addLayer(boardLayer);
		chatLayer = new ChatLayer(width, height);
		addLayer(chatLayer, 1, false);
		dialogLayer = new DialogLayer();
		dialogLayer.setDialogListener(PlayScreen.this);
		addLayer(dialogLayer, 2, false);
		setDefaultInputListener();
		// }
		boardLayer.init(true);
		boardLayer.joinedRoom = false;
		// boardLayer.joinRoomSuccess(boardLayer.jsonJoinRoom);
		MyPoker.client.resume();
	}

	@Override
	protected void setAssetNeed() {
		addAsset(PokerTexture.inst());

	}

	public void updateMoney(JSONObject json) {
		// TODO Auto-generated method stub
		if (boardLayer != null && MyPoker.game.statePortal.equals(StatePortal.IN_GAME))
			boardLayer.updateMoney(json);
	}

	@Override
	public GsnGame getGame() {
		// TODO Auto-generated method stub
		return MyPoker.game;
	}

	public void showChatLayer() {
		chatLayer.show();
	}
	
	public void onBackPress()
	{
		boardLayer.onBackPress();
	}
}
